<template>
  <div class="app-container">
    <bread parentPathText="信访管理" currentPathText="信访事件管理" />
    <el-form :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="日期选择">
        <el-date-picker
          v-model="date"
          type="daterange"
          value-format="yyyy-MM-dd"
          size="mini"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="信访方式">
        <el-select
          v-model="queryParams.method"
          placeholder="请选择方式"
          clearable
          size="small"
          style="width: 260px"
        >
          <el-option
            v-for="item in methodData"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="办理结果">
        <el-select
          v-model="queryParams.result"
          placeholder="请选择办理结果"
          clearable
          size="small"
          style="width: 260px"
        >
          <el-option
            v-for="item in resultData"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="筛选类型">
        <el-select
          v-model="queryParams.type"
          placeholder="选择类型"
          clearable
          size="small"
          style="width: 260px"
        >
          <el-option
            v-for="item in types"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="queryParams.info"
          placeholder="请输入姓名/电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <div class="addItem">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd"
        >新增</el-button
      >
    </div>
    <el-table v-loading="loading" :data="tableList">
      <el-table-column
        align="center"
        label="信访人"
        prop="userName"
        min-width="20%"
      />
      <el-table-column
        align="center"
        label="信访事件"
        prop="event"
        :show-overflow-tooltip="true"
        min-width="15%"
      />
      <el-table-column
        align="center"
        label="问题发生地"
        prop="address"
        :show-overflow-tooltip="true"
        min-width="20%"
      />
      <el-table-column
        align="center"
        label="经办人"
        prop="dealPeople"
        min-width="20%"
      />
      <el-table-column
        align="center"
        label="办理方式"
        prop="dealMethod"
        min-width="30%"
      />
      <el-table-column
        align="center"
        label="去向"
        prop="whereAbout"
        min-width="15%"
      />
      <el-table-column
        align="center"
        label="办理结果"
        prop="dealResultVal"
        min-width="15%"
      />
      <el-table-column
        align="center"
        label="信访日期"
        prop="visitTime"
        min-width="15%"
      />
      <el-table-column
        align="center"
        label="操作"
        min-width="25%"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加/编辑弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      append-to-body
    >
      <el-form :model="ruleForm"  :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="信访人" prop="userName">
          <el-input
            v-model="ruleForm.userName"
            placeholder="请输入信访人"
          ></el-input>
        </el-form-item>
        <el-form-item label="信访人数" prop="num">
          <el-input-number
            v-model="ruleForm.num"
            type="number"
            :min="1"
            style="width:100%;"
            placeholder="请输入信访人数"
          />
        </el-form-item>
        <el-form-item label="信访事件" prop="event">
           <el-input
            v-model="ruleForm.event"          
          ></el-input>
        </el-form-item>
        <el-form-item label="信访方式" prop="pattern">
          <el-select
            v-model="ruleForm.pattern"
            placeholder="请选择方式"
            clearable
            size="small"
            style="width: 260px"
            @change="popPatternChange"
          >
            <el-option
              v-for="item in methodData1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="办理结果" prop="dealResult">
          <el-select
            v-model="ruleForm.dealResult"
            placeholder="请选择办理结果"
            clearable
            size="small"
            style="width: 260px"
            @change="popResultChange"
          >
            <el-option
              v-for="item in resultData1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="问题发生地" prop="address">
          <el-input
            v-model="ruleForm.address"
            placeholder="请输入问题发生地"
          ></el-input>
        </el-form-item>
        <el-form-item label="办理方式" prop="dealMethod">
          <el-input
            v-model="ruleForm.dealMethod"
            placeholder="请输入办理方式"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="tel">
          <el-input
            v-model="ruleForm.tel"
            placeholder="请输入联系电话"
          ></el-input>
        </el-form-item>
        <el-form-item label="信访内容" prop="eventDescription">
          <el-input
            v-model="ruleForm.eventDescription"
            placeholder="请输入信访内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="登记单位" prop="registUnit">
          <el-input
            v-model="ruleForm.registUnit"
            placeholder="请输入登记单位"
          ></el-input>
        </el-form-item>
        <el-form-item label="经办人" prop="dealPeople">
          <el-input
            v-model="ruleForm.dealPeople"
            placeholder="请输入经办人"
          ></el-input>
        </el-form-item>
        <el-form-item label="去向" prop="whereAbout">
          <el-input
            v-model="ruleForm.whereAbout"
            placeholder="请输入去向"
          ></el-input>
        </el-form-item>
        <el-form-item label="信访日期" prop="visitTime">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="ruleForm.visitTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="文件上传">
          <el-upload
            class="demo"
            :action="actionUrl"
            :on-preview="handlePreview"
            :limit="1"
            :on-exceed="handleExceed"
            :on-success="handleSuccess"
            accept=".pdf, .doc, .docx,.jpg,.jpeg,.png"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确定</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </div>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      class="pagination"
    >
    </el-pagination>

    <el-dialog title="图片" :visible.sync="dialogVisible1" width="30%">
      <div style="text-align: center">
        <el-image :src="imgUrl"></el-image>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import initPagin from '../mixins/index.js';
export default {
  mixins:[initPagin],
  data() {
    return {
      dialogVisible1: false,
      imgUrl: "",
      date: "",
      queryParams: {
        method: "",
        type: "",
        info: "",
        result: "",
      },
      fileList: [],
      fileInfo: "",
      fileName: "",
      methodData: [
        {
          label: "全部",
          value: '',
        },
        {
          label: "申述信",
          value: 1,
        },
        {
          label: "来电申述",
          value: 2,
        },
        {
          label: "当面申述",
          value: 3,
        },
        {
          label: "网上申述",
          value: 4,
        },
      ],
      methodData1: [
        {
          label: "申述信",
          value: 1,
        },
        {
          label: "来电申述",
          value: 2,
        },
        {
          label: "当面申述",
          value: 3,
        },
        {
          label: "网上申述",
          value: 4,
        },
      ],
      resultData: [
        {
          label: "全部",
          value: 0,
        },
        {
          label: "已转送",
          value: 1,
        },
        {
          label: "已办理",
          value: 2,
        },
        {
          label: "未办理",
          value: 3,
        },
      ],
      resultData1: [
        {
          label: "已转送",
          value: 1,
        },
        {
          label: "已办理",
          value: 2,
        },
        {
          label: "未办理",
          value: 3,
        },
      ],
      types: [
        {
          label: "全部",
          value: 0,
        },
        {
          label: "姓名",
          value: 1,
        },
        {
          label: "电话",
          value: 2,
        },
      ],
      loading: false,
      // 弹框
      title: "添加",
      dialogVisible: false,
      ruleForm: {
        userName: "",
        num: "",
        event: "",
        address: "",
        pattern: "",
        dealMethod: "",
        dealResult: "",
        tel: "",
        eventDescription: "",
        registUnit: "",
        dealPeople: "",
        whereAbout: "",
        visitTime: "",
      },
      rules: {
        userName: [
          { required: true, message: "请输入信访人", trigger: "blur" },
        ],
        num: [
          { required: true, message: "请输入信访人数", trigger: "blur" },
        ],
        event: [
          { required: true, message: "请输入信访事件", trigger: "blur" },
        ],
      },
      time: new Date().getTime(),
      id: "",
    };
  },
  computed: {
    actionUrl() {
      if (process.env.NODE_ENV == "development") {
        return "http://192.168.1.111:10001/api/ueditor/uploadExtraFile";
      } else {
        return `http://web.sanfengcun.com/api/ueditor/uploadExtraFile`;
      }
    },
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 搜索按钮操作
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },
    // 重置按钮操作
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => {
        this.queryParams[key] = "";
      });
      this.date = "";
    },
    // 获取列表
    getList() {
      this.loading = true;
      let params = {
        time: this.time,
        pageNum:this.pageNum,
        pageSize:this.pageSize,
        dealResult: this.queryParams.result,
        startTime: this.date ? this.date[0] : "",
        endTime: this.date ? this.date[1] : "",
        status: this.queryParams.type,
        pattern: this.queryParams.method,
        queryMsg: this.queryParams.info,
      };
      let result = this.$md5.transferValue(params);
      this.axios
        .post(this.stateUrl.petition.list, {
          time: this.time,
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          dealResult: this.queryParams.result,
          startTime: this.date ? this.date[0] : "",
          endTime: this.date ? this.date[1] : "",
          status: this.queryParams.type,
          pattern: this.queryParams.method,
          queryMsg: this.queryParams.info,
          sign: this.$md5.val(result),
        })
        .then((res) => {
          this.loading = false;
          this.tableList = res.data.data;
          this.total = res.data.count;
        })
        .catch(() => {});
    },
    // 添加
    handleAdd() {
      this.dialogVisible = true;
      this.title = "新增";
      this.fileList = [];
      this.id = "";
      Object.keys(this.ruleForm).forEach((key) => {
        this.ruleForm[key] = "";
      });
    },
    // 编辑
    handleUpdate(row) {
      this.dialogVisible = true;
      this.title = "编辑";
      this.id = row.id;
      let params = {
        time: this.time,
        id: row.id,
      };
      let result = this.$md5.transferValue(params);
      this.axios
        .post(this.stateUrl.petition.detail, {
          time: this.time,
          id: row.id,
          sign: this.$md5.val(result),
        })
        .then((res) => {
          this.ruleForm.userName = res.data.data.userName;
          this.ruleForm.num = res.data.data.num;
          this.ruleForm.event = res.data.data.event;
          this.ruleForm.address = res.data.data.address;
          this.ruleForm.pattern = res.data.data.pattern;
          this.ruleForm.dealMethod = res.data.data.dealMethod;
          this.ruleForm.dealResult = res.data.data.dealResult;
          this.ruleForm.tel = res.data.data.tel;
          this.ruleForm.eventDescription = res.data.data.eventDescription;
          this.ruleForm.registUnit = res.data.data.registUnit;
          this.ruleForm.dealPeople = res.data.data.dealPeople;
          this.ruleForm.whereAbout = res.data.data.whereAbout;
          this.ruleForm.visitTime = res.data.data.visitTime;
          this.fileList = [
            {
              name: res.data.data.accessoryName,
              url: res.data.data.accessoryPath,
            },
          ];
        })
        .catch(() => {});
    },
    // 删除按钮操作
    handleDelete(row) {
      let that = this;
      this.$confirm("是否确认删除?", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          that.delRole(row.id);
        })
        .catch(() => {});
    },
    // 删除
    delRole(id) {
      let params = {
        time: this.time,
        id,
      };
      let result = this.$md5.transferValue(params);
      this.axios
        .post(this.stateUrl.petition.delete, {
          time: this.time,
          id,
          sign: this.$md5.val(result),
        })
        .then((res) => {
          this.msgSuccess("删除成功");
          this.getList();
        })
        .catch(() => {});
    },
    // 弹框信访方式
    popPatternChange(val) {
      this.ruleForm.pattern = val;
    },
    // 弹框选择办理结果
    popResultChange(val) {
      this.ruleForm.dealResult = val;
    },
    handleExceed() {
      this.$message.warning(`最多只能上传一个附件`);
    },
    handleSuccess(res) {
      this.fileInfo = res.data.filePath;
      this.fileName = res.data.fileName;
    },
    handlePreview(file) {
      let data = file.url ? file.url : file.response.data.filePath;
      if (
        data.includes(".jpg") ||
        data.includes(".png") ||
        data.includes(".jpeg")
      ) {
        this.dialogVisible1 = true;
        this.imgUrl = data;
      } else {
        let link = document.createElement("a");
        link.href = data;
        document.body.appendChild(link);
        link.click(); //执行a标签
      }
    },
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            id: this.id,
            time: this.time,
            userName: this.ruleForm.userName,
            num: this.ruleForm.num,
            pattern: this.ruleForm.pattern,
            address: this.ruleForm.address,
            tel: this.ruleForm.tel,
            event: this.ruleForm.event,
            eventDescription: this.ruleForm.eventDescription,
            registUnit: this.ruleForm.registUnit,
            dealPeople: this.ruleForm.dealPeople,
            dealMethod: this.ruleForm.dealMethod,
            dealResult: this.ruleForm.dealResult,
            whereAbout: this.ruleForm.whereAbout,
            accessoryName: this.fileName,
            accessoryPath: this.fileInfo,
            visitTime: this.ruleForm.visitTime,
          };
          let result = this.$md5.transferValue(params);
          let url = "";
          if (!this.id) {
            url = this.stateUrl.petition.add;
          } else {
            url = this.stateUrl.petition.edit;
          }
          this.axios
            .post(url, {
              id: this.id,
              time: this.time,
              userName: this.ruleForm.userName,
              num: this.ruleForm.num,
              pattern: this.ruleForm.pattern,
              address: this.ruleForm.address,
              tel: this.ruleForm.tel,
              event: this.ruleForm.event,
              eventDescription: this.ruleForm.eventDescription,
              registUnit: this.ruleForm.registUnit,
              dealPeople: this.ruleForm.dealPeople,
              dealMethod: this.ruleForm.dealMethod,
              dealResult: this.ruleForm.dealResult,
              whereAbout: this.ruleForm.whereAbout,
              visitTime: this.ruleForm.visitTime,
              accessoryName: this.fileName,
              accessoryPath: this.fileInfo,
              sign: this.$md5.val(result),
            })
            .then((res) => {
              this.msgSuccess("操作成功");
              this.dialogVisible = false;
              this.getList();
            })
            .catch(() => {});

        }
      })
    },
    handleClose() {
      this.$refs["ruleForm"].resetFields();
      this.dialogVisible = false;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.fileList = [];
    },
  },
};
</script>
<style lang="less" scoped>
/deep/ .el-select,
/deep/ .el-date-editor {
  width: 100% !important;
}
/deep/ .el-dialog__body {
  max-height: 500px;
  overflow: auto;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>